﻿using MediatR;
using WMS_Goodsinfo.Api.Read.Application.Command;
using WMS_Goodsinfo.Api.Read.DTOS;
using WMS_Goodsinfo.Domain;
using WMS_Goodsinfo.ErrorCode;
using WMS_Goodsinfo.Infastructure;

namespace WMS_Goodsinfo.Api.Read.Application.Handler
{
    public class SelectShelflifeHandler : IRequestHandler<SelectShelflifeCommand, ApiPaging<ShlflifeDto>>
    {
        private readonly IProductRepository<Product_Table> _productRepo;

        public SelectShelflifeHandler(IProductRepository<Product_Table> productRepo)
        {
            _productRepo = productRepo;
        }
        /// <summary>
        /// 保质期查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiPaging<ShlflifeDto>> Handle(SelectShelflifeCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ShlflifeDto> result = new ApiPaging<ShlflifeDto>();

            var query = _productRepo.GetAll().Where(p => !p.IsDelete);

            // 应用过滤条件
            if (!string.IsNullOrEmpty(request.ProductName))
            {
                query = query.Where(p => p.ProductName.Contains(request.ProductName));
            }

            if (!string.IsNullOrEmpty(request.ProductTypeId))
            {
                query = query.Where(p => p.ProductTypeId == request.ProductTypeId);
            }

            if (!string.IsNullOrEmpty(request.UnitName))
            {
                query = query.Where(p => p.UnitName.Contains(request.UnitName));
            }

            if (request.Status.HasValue)
            {
                query = query.Where(p => p.Status == request.Status.Value);
            }

            if (!string.IsNullOrEmpty(request.Inventory))
            {
                query = query.Where(p => p.Inventory.Contains(request.Inventory));
            }

            if (request.Remainingdays.HasValue)
            {
                query = query.Where(p => p.Remainingdays <= request.Remainingdays.Value);
            }

            if (!string.IsNullOrEmpty(request.Batch))
            {
                query = query.Where(p => p.Batch.Contains(request.Batch));
            }

            if (!string.IsNullOrEmpty(request.Barcode))
            {
                query = query.Where(p => p.Barcode.Contains(request.Barcode));
            }

            

            // 获取总数
            var totalCount = query.Count();

            // 分页查询
            var pageData = query
                .Skip((request.Pageindex - 1) * request.Pagesize)
                .Take(request.Pagesize)
                .Select(x => new ShlflifeDto
                {
                    ProductNo = x.ProductNo,
                    ProductName = x.ProductName,
                    ProductTypeId = x.ProductTypeId,
                    ProductModel = x.ProductModel,
                    Status = x.Status,
                    UnitName = x.UnitName,
                    Barcode = x.Barcode,
                    Inventory = x.Inventory,
                    Location = x.Location,
                    Current_Inventory = x.Current_Inventory,
                    TotalMoeny = x.TotalMoeny,
                    Remarks = x.Remarks,
                    Batch = x.Batch,
                    Productiondate = x.Productiondate,
                    Expirationdate = x.Expirationdate,
                    Shelflifedays = x.Shelflifedays,
                    Remainingdays = x.Remainingdays,
                    Alertdays = x.Alertdays
                })
                .ToList();

            result.Code = StatusEnum.OK;
            result.Message = "查询成功";
            result.TotalCount = totalCount;
            result.PageData = pageData;

            return result;
        }
    }
}
